package com.example.education.controller;

import com.example.education.common.ResponseBuilder;
import com.example.education.exception.ParamException;
import com.example.education.model.Evaluate;
import com.example.education.model.User;
import com.example.education.service.EvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/interface/evaluate")
public class EvaluateController {

    @Autowired
    EvaluateService evaluateService;

    @RequestMapping("/insert")
    public boolean insertEvaluate(Evaluate evaluate, HttpSession session){
        boolean result = false;
        try {
            User user = (User) session.getAttribute("user");
            evaluate.setUserId(user.getUserId());
            evaluate.setUserName(user.getLoginName());
            evaluateService.insertEvaluate(evaluate);
            System.out.println("插入成功!");
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/like")
    public boolean likeEvaluate(Integer evaluateId){
        boolean result = false;
        try {
            result = evaluateService.likeEvaluate(evaluateId);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/delete")
    public boolean deleteEvaluate(Integer evaluateId){
        boolean result = false;
        try {
            evaluateService.deleteEvaluate(evaluateId);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return result;
    }
    @RequestMapping("/top")

    public boolean topEvaluate(Integer evaluateId){
        boolean result = false;
        try {
            result = evaluateService.topEvaluate(evaluateId);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/unTop")
    public boolean unTopEvaluate(Integer evaluateId){
        boolean result = false;
        try {
            result = evaluateService.unTopEvaluate(evaluateId);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/getById")
    public Evaluate getEvaluateById(Integer evaluateId){
        Evaluate evaluate = new Evaluate();
        try {
            evaluate = evaluateService.getEvaluateById(evaluateId);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return evaluate;
    }

    @RequestMapping("/getByCourseId")
    public List<Evaluate> getEvaluateByCourseId(Integer courseId){
        List<Evaluate> evaluates = new ArrayList<>();
        try {
            evaluates = evaluateService.getEvaluateByCourseId(courseId);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return evaluates;
    }

    @RequestMapping("getAll")
    @ResponseBody
    public ResponseBuilder getAllByPage(Integer page, Integer limit) {
        try {
            int allCount = evaluateService.getAllCount();
            List<Evaluate> evaluateList = evaluateService.getAll(page, limit);
            return ResponseBuilder.buildPage("成功", evaluateList, allCount);
        } catch (ParamException e) {
            e.printStackTrace();
            return ResponseBuilder.buildError("失败,异常:" + e.getMessage());
        }
    }
}
